package net.expectx.pay.controller;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiOperation;

import net.expectx.common.single.base.BaseConstants;
import net.expectx.common.single.base.BaseResult;
import net.expectx.common.single.util.AesUtil;
import net.expectx.common.single.util.Md5Util;
import net.expectx.common.single.util.NetStateUtil;
import net.expectx.common.single.util.RedisUtil;
import net.expectx.pay.common.AdminConstants;
import net.expectx.pay.common.AdminResult;
import net.expectx.pay.common.util.ExpectXUtil;
import net.expectx.pay.common.util.UserAgentUtil;
import net.expectx.pay.core.api.PaymentHandlerInterface;
import net.expectx.pay.core.common.EXPayResult;
import net.expectx.pay.core.service.provider.ProviderAliPayPageDriver;
import net.expectx.pay.core.service.provider.ProviderAliPayQrCodeDriver;
import net.expectx.pay.core.service.provider.ProviderAliPayWapDriver;
import net.expectx.pay.dao.bean.*;
import net.expectx.pay.rpc.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author lijian
 */
@Controller
@RequestMapping("/pay/")
public class PayController {
    /*public static String APPID = "2018081661057690";
    public static String RSA_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDMjsT0YORzF1IFhGu4TEx0LF2d/OyjyQsg6qEkkfuNKRqhlw8+D8npH4NpE9M01JQbE5yRFD/2U8hOmLccB2Yh6FXNxEt3KXAHLN6ARxjqHi+wRwF4V4hPRK09K+OmYCGZK86Jgt/zoY11HD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a6+l+yUeGbDIJTN4QwKBgBbj6Z9eHWNjLtXvwCPToOclAFbTqq/IpqodTMNkN5UN805SEASIYL5FbZsRCYkiBUJ3jWSULbYm3VAPZPNyTR1h4nO74JgVQkLkBc9H9wEp2H11RwNxAMVEfxuqioazDcLuymErtTdCyILHNlVczE5gjXmAmlryN7X0tUrveHtlAoGASQ8z7xk9b0LJTZr4xuqdd2F4AQdF+mfGfmKwiJJPS8YKu1ibv/S0iwWdNJWwadcQnAFE/UKR56C+34n0o/5oD7TASoMfv4jNK9zgLSdT+DAY/T4WpcYWW+kT5V94to0+NbsYPu84j1kq9rQVttadOPT9AgL+qio15esy2Tqz2XA=";
    public static String URL = "https://openapi.alipay.com/gateway.do";
    public static String CHARSET = "UTF-8";
    public static String FORMAT = "json";
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5NCFAcO7OV9xW3iaXt7dSbjtsBGKKeW2VTrT0oBOK0LwHFrkY9OBRldeun1zjlFoty+fdLVu9lIqiwKMOJ3sA/tlr3swk0cXWeV0m4nvzK9t2ZbHEl8+J6iHtraMMnyzXEaBx9CZLZHKVVHnCViLIXQoQoANevk6oBMAN/jz38EwEJaG0/xn4dshL/KsNp6Gp4aQuDecZKNy4KGP17QSy1RPDz47vvKue2lyFsKT/Y1xKgyVPBw6sAlED/xTqD+Dj8lJFr825s1Uc2v3uM6XIgkSPM3JcCwUVEllMtNRHug2BoePb2frjaC3Sj+akeTZ4aYer20yXxB0Tp8VQPEScQIDAQAB";
    public static String SIGNTYPE = "RSA2";*/

    /**
     * Expect-X
     */
   /* public static String APPID ="2019082666447672";
    public static String RSA_PRIVATE_KEY = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDBS24i9cCsb6azxcGvWirb9y+i+Kwx0NxvP6PT3/OwSrL3xh6dEEsiEMS2xqb+rpaA1Wg56T9SA+Akc46Npue7HTMlC0Jmeoc08+tF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K1k/hPrT2Fn/n99wqGPgUnXlIBoWrdAoGASFdqfeF8Bs+69DnyRgXRotYAQPZEuUZBbOms6Po+4Q0NLlwkDCibLnc8ohSHqttOPRIz9hlX8MsZfXzGl+Jwz9xZ0eydlWx0Cga6YiTxd+8eGwWg08pot+5SEiPw4Ngi6ox5x0t/e0J7KQwQwOZmhZMRDqDRQpx6RJYWDdtNxgECgYBSiooQhg+chHRCxDDeGPMpFY0AyE8cKcSvqdM3E2R6U70w4/iAuMF7C22CODwNaSRRJ/1JoU2dO2MzxV13hqwN+BWPemNxl+j3o0Zju3Gd2IXxiGNDp7dX6FrUYukAqqxOwOt+maS9W7hIhthyGnnRXOEvNcCMokY/8ubsRrv0+w==";
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu7qWAwSW+cq7eznCEaHBn1NWH6auw/Pg1cznqqvTFvBm6zaUCvWyoKIU7GD5MmQkVLlfAF7B5GhOq5LCoGvwGTmNn1zlA1O6ustajVH6/S7Bf375sFgei50u4elSjyenbB9yp+zjEVcv0t6xKaqyoZQ7+B6VScRiblZN/XphNXC1zpl48JlU7WDmNBLL9B09FnLULcLvIIrznXGZY3bX/3Qv5gJKK/VQGWKBFLfcYDfCkF/4eOEf8ezK6izWGvXOCVman/QTHU286pJIzywiVJPZ9S+tWtsBi/kHlvkSX77LKgwZn2bk+rRNcYTUdZ4OPy5YccW/8XtYldHRP6X2mQIDAQAB";
    public static String SIGNTYPE = "RSA2";
    public static String URL = "https://openapi.alipay.com/gateway.do";
    public static String CHARSET = "UTF-8";
    public static String FORMAT = "json";*/

    public static String APPID ="2019070265746442";
    public static String RSA_PRIVATE_KEY = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDBS24i9cCsb6azxcGvWirb9y+i+Kwx0NxvP6PT3/OwSrL3xh6dEEsiEMS2xqb+rpaA1Wg56T9SA+Akc46Npue7HTMlC0Jmeoc08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XV3K1k/hPrT2Fn/n99wqGPgUnXlIBoWrdAoGASFdqfeF8Bs+69DnyRgXRotYAQPZEuUZBbOms6Po+4Q0NLlwkDCibLnc8ohSHqttOPRIz9hlX8MsZfXzGl+Jwz9xZ0eydlWx0Cga6YiTxd+8eGwWg08pot+5SEiPw4Ngi6ox5x0t/e0J7KQwQwOZmhZMRDqDRQpx6RJYWDdtNxgECgYBSiooQhg+chHRCxDDeGPMpFY0AyE8cKcSvqdM3E2R6U70w4/iAuMF7C22CODwNaSRRJ/1JoU2dO2MzxV13hqwN+BWPemNxl+j3o0Zju3Gd2IXxiGNDp7dX6FrUYukAqqxOwOt+maS9W7hIhthyGnnRXOEvNcCMokY/8ubsRrv0+w==";
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5NCFAcO7OV9xW3iaXt7dSbjtsBGKKeW2VTrT0oBOK0LwHFrkY9OBRldeun1zjlFoty+fdLVu9lIqiwKMOJ3sA/tlr3swk0cXWeV0m4nvzK9t2ZbHEl8+J6iHtraMMnyzXEaBx9CZLZHKVVHnCViLIXQoQoANevk6oBMAN/jz38EwEJaG0/xn4dshL/KsNp6Gp4aQuDecZKNy4KGP17QSy1RPDz47vvKue2lyFsKT/Y1xKgyVPBw6sAlED/xTqD+Dj8lJFr825s1Uc2v3uM6XIgkSPM3JcCwUVEllMtNRHug2BoePb2frjaC3Sj+akeTZ4aYer20yXxB0Tp8VQPEScQIDAQAB";
    public static String SIGNTYPE = "RSA2";
    public static String URL = "https://openapi.alipay.com/gateway.do";
    public static String CHARSET = "UTF-8";
    public static String FORMAT = "json";



    @ApiOperation(value = "查询")
    @GetMapping("doQuery")
    @ResponseBody
    public Object doQuery(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            String tradeNo = request.getParameter("tradeNo");
            Map<String, String> config = new HashMap<>(2);
            config.put("URL", URL);
            config.put("APP_ID", APPID);
            config.put("RSA_PRIVATE_KEY", RSA_PRIVATE_KEY);
            config.put("FORMAT", FORMAT);
            config.put("CHARSET", CHARSET);
            config.put("ALI_PAY_PUBLIC_KEY", ALIPAY_PUBLIC_KEY);
            config.put("SIGN_TYPE", SIGNTYPE);
            PaymentHandlerInterface paymentHandlerInterfac = new ProviderAliPayWapDriver(config);
            Map<String, Object> params = new HashMap<>(2);
            params.put("tradeNo", tradeNo);
            return new AdminResult(paymentHandlerInterfac.doQuery(params));
        } catch (Exception e) {
            return new AdminResult(Boolean.FALSE, BaseConstants.FAILE, "系统错误:" + e.getLocalizedMessage(), null);
        }
    }

    @ApiOperation(value = "退款")
    @PostMapping("doRefund")
    @ResponseBody
    public Object doRefund(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            String tradeNo = request.getParameter("tradeNo");
            String refundAmount = request.getParameter("refundAmount");
            String refundReason = request.getParameter("refundReason");
            Map<String, String> config = new HashMap<>(2);
            config.put("URL", URL);
            config.put("APP_ID", APPID);
            config.put("RSA_PRIVATE_KEY", RSA_PRIVATE_KEY);
            config.put("FORMAT", FORMAT);
            config.put("CHARSET", CHARSET);
            config.put("ALI_PAY_PUBLIC_KEY", ALIPAY_PUBLIC_KEY);
            config.put("SIGN_TYPE", SIGNTYPE);
            PaymentHandlerInterface paymentHandlerInterfac = new ProviderAliPayWapDriver(config);
            Map<String, Object> params = new HashMap<>(2);

            params.put("tradeNo", tradeNo);
            params.put("refundAmount", refundAmount);
            params.put("refundReason", refundReason);
            return new AdminResult(paymentHandlerInterfac.doRefund(params));
        } catch (Exception e) {
            return new AdminResult(Boolean.FALSE, BaseConstants.FAILE, "系统错误:" + e.getLocalizedMessage(), null);
        }
    }




}
